define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'clothmaking_product/index' + location.search,
                    add_url: 'clothmaking_product/add',
                    edit_url: 'clothmaking_product/edit',
                    del_url: 'clothmaking_product/del',
                    multi_url: 'clothmaking_product/multi',
                    import_url: 'clothmaking_product/import',
                    table: 'clothmaking_product',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'package_material_id', title: __('Package_material_id')},
                        {field: 'number', title: __('Number'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE'},
                        {field: 'alias', title: __('Alias'), operate: 'LIKE'},
                        {field: 'specifications', title: __('Specifications'), operate: 'LIKE'},
                        {field: 'thickness1', title: __('Thickness1'), operate:'BETWEEN'},
                        {field: 'thickness1_standard', title: __('Thickness1_standard'), operate:'BETWEEN'},
                        {field: 'thickness2', title: __('Thickness2'), operate:'BETWEEN'},
                        {field: 'thickness2_theory', title: __('Thickness2_theory'), operate:'BETWEEN'},
                        {field: 'width', title: __('Width'), operate:'BETWEEN'},
                        {field: 'length', title: __('Length'), operate:'BETWEEN'},
                        {field: 'color', title: __('Color'), operate: 'LIKE'},
                        {field: 'density1', title: __('Density1'), operate:'BETWEEN'},
                        {field: 'density1_standard', title: __('Density1_standard'), operate:'BETWEEN'},
                        {field: 'density2', title: __('Density2'), operate:'BETWEEN'},
                        {field: 'density2_theory', title: __('Density2_theory'), operate:'BETWEEN'},
                        {field: 'tear_strength', title: __('Tear_strength'), operate:'BETWEEN'},
                        {field: 'tensile_strength', title: __('Tensile_strength'), operate:'BETWEEN'},
                        {field: 'hardness', title: __('Hardness'), operate: 'LIKE'},
                        {field: 'material_structure', title: __('Material_structure'), operate: 'LIKE'},
                        {field: 'membrane_type', title: __('Membrane_type'), operate: 'LIKE'},
                        {field: 'surface_treatment', title: __('Surface_treatment'), operate: 'LIKE'},
                        {field: 'spring_back', title: __('Spring_back'), operate: 'LIKE'},
                        {field: 'stress_requirement', title: __('Stress_requirement'), operate: 'LIKE'},
                        {field: 'demand', title: __('Demand'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));

                // 计算偏差范围
                function calculateDeviations() {
                    // 厚度1偏差范围：基于厚度1值的默认偏差
                    var thickness1 = parseFloat($("#c-thickness1").val());
                    var thickness1Standard = parseFloat($("#c-thickness1_standard").val());
                    if (!isNaN(thickness1) && !isNaN(thickness1Standard)) {
                        var deviation1 = thickness1 * (thickness1Standard / 100); // 标准值为百分比
                        var min1 = (thickness1 - deviation1).toFixed(3);
                        var max1 = (thickness1 + deviation1).toFixed(3);
                        $("#c-thickness1-deviation").val(min1 + " - " + max1);
                    }

                    // 厚度2偏差范围：基于厚度2值的理论值偏差
                    var thickness2 = parseFloat($("#c-thickness1").val());
                    var thickness2Theory = parseFloat($("#c-thickness2_theory").val());
                    if (!isNaN(thickness2) && !isNaN(thickness2Theory)) {
                        var deviation2 = thickness2 * (thickness2Theory / 100); // 理论值为百分比
                        var min2 = (thickness2 - deviation2).toFixed(3);
                        var max2 = (thickness2 + deviation2).toFixed(3);
                        $("#c-thickness2-deviation").val(min2 + " - " + max2);
                    }

                    // 密度1偏差范围：基于密度1值的默认偏差
                    var density1 = parseFloat($("#c-density1").val());
                    var density1Standard = parseFloat($("#c-density1_standard").val());
                    if (!isNaN(density1) && !isNaN(density1Standard)) {
                        var deviation3 = density1 * (density1Standard / 100); // 标准值为百分比
                        var min3 = (density1 - deviation3).toFixed(3);
                        var max3 = (density1 + deviation3).toFixed(3);
                        $("#c-density1-deviation").val(min3 + " - " + max3);
                    }

                    // 密度2偏差范围：基于密度2值的理论值偏差
                    var density2 = parseFloat($("#c-density1").val());
                    var density2Theory = parseFloat($("#c-density2_theory").val());
                    if (!isNaN(density2) && !isNaN(density2Theory)) {
                        var deviation4 = density2 * (density2Theory / 100); // 理论值为百分比
                        var min4 = (density2 - deviation4).toFixed(3);
                        var max4 = (density2 + deviation4).toFixed(3);
                        $("#c-density2-deviation").val(min4 + " - " + max4);
                    }
                }

                // 绑定所有相关输入事件
                $("#c-density1, #c-thickness1, #c-density1_standard, #c-thickness1_standard, #c-thickness2_theory, #c-density2_theory").on("input change", function() {
                    calculateDeviations();
                });
                // 页面加载时触发计算
                calculateDeviations();
            }
        }
    };

  
    return Controller;
});